package com.pureland.security.filter;

import com.pureland.security.ResourceAuthExceptionEntryPoint;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;
import org.springframework.stereotype.Component;

/**
 * @projectName: pureland_cloud
 * @className: com.pureland.security.filter.DefaultClientCredentialsTokenEndpointFilter
 * @description: 自定义客户端信任过滤器
 * @author: tong.li
 * @createTime: 2020/11/27 15:58
 * @version: v1.0
 * @copyright: 版权所有 李彤 © 2020
 */
@Component
@Data
public class DefaultClientCredentialsTokenEndpointFilter extends ClientCredentialsTokenEndpointFilter {


    private AuthorizationServerSecurityConfigurer configurer;

    @Autowired
    private ResourceAuthExceptionEntryPoint authenticationEntryPoint;



    @Override
    protected AuthenticationManager getAuthenticationManager() {
        return configurer.and().getSharedObject(AuthenticationManager.class);
    }

    @Override
    public void afterPropertiesSet() {
        setAuthenticationFailureHandler((request, response, exception) -> authenticationEntryPoint.commence(request, response, exception));
        setAuthenticationSuccessHandler((request, response, authentication) -> {
        });
    }

}
